# Maintainer: Peter Budai <peterbudai at hotmail.com>

_realname=clblast
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.5.1
pkgrel=1
pkgdesc="Tuned OpenCL BLAS (mingw-w64)"
arch=('any')
url='https://github.com/CNugteren/CLBlast'
license=('Apache 2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-opencl-headers"
            "${MINGW_PACKAGE_PREFIX}-opencl-icd")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/CNugteren/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('1679843290efdeafdc187c07dc033e27067b37e34243f9417087700f61408069')

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  make
}


check() {
  cd "${srcdir}"/build-${CARCH}
  #make check
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # move the dll to bin
  mv "${pkgdir}${MINGW_PREFIX}/lib/libclblast.dll" "${pkgdir}${MINGW_PREFIX}/bin"

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/CLBlast/CLBlastConfig.cmake \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/CLBlast/CLBlastConfig-release.cmake
}
